package engineer.clientapp.tools;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;

public class ShowAutomats {

	public static ArrayList<String> showAutomats() {
		ArrayList<String> automats = new ArrayList<String>();
		ArrayList<String> cola = new ArrayList<String>();
		try {
			Connection connection = new Connector().getConnection();
			Statement statement = connection.createStatement();
			Statement statement2 = connection.createStatement();

			ResultSet result = statement
					.executeQuery("SELECT * FROM automats.machines order by id");

			while (result.next()) {
				String name = result.getString("machines.name");
				String address = result.getString("machines.address");
				String max_amount = result.getString("machines.max_amount");
				String current_amount = result
						.getString("machines.current_amount");
				int id = Integer.parseInt(result.getString("machines.id"));
				String image = result.getString("image");
				ResultSet result2 = statement2
						.executeQuery("select machines_trademarks.current_amount, dictionary_trademarks.name, machines_trademarks.machine_id from automats.machines_trademarks join automats.dictionary_trademarks on machines_trademarks.trademark_id=dictionary_trademarks.id where machines_trademarks.machine_id="
								+ id);
				while (result2.next()) {

					cola.add(result2
							.getString("machines_trademarks.current_amount"));
					// automats.add(image);
				}
				automats.add("<li><h3>"
						+ name
						+ "</h3><table><tr><td rowspan='10'><div class='image_wrapper image_fl'><img src='images/automats/"
						+ image
						+ "' alt='image' class='image_frame_100' /><tr><td><b>ID:</b></td><td> "
						+ id
						+ "</td></tr><tr><td><b>Adres:</b></td><td>"
						+ address
						+ "</td></tr><tr><td><b>Maksymalna pojemność:</b></td><td> "
						+ max_amount
						+ "</td></tr><tr><td><b>Aktualny stan:</b></td><td> "
						+ current_amount
						+ "</td></tr><tr><td><b>Coca Cola:</b></td><td>"
						+ cola.get(0) + "</td></tr><tr><td><b>Fanta:</b></td><td>"
						+ cola.get(1) + "</td></tr><tr><td><b>Sprite:</b></td><td>"
						+ cola.get(2) + "</td></tr><tr><td><b>Nestea:</b></td><td>"
						+ cola.get(3) + "</td></tr><tr><td><b>Vitel:</b></td><td>"
						+ cola.get(4) + "</td></tr></table></li><br>");
				cola.clear();
			}
			result.close();
			connection.close();
		} catch (Exception e) {
			e.printStackTrace();
			automats.add("Wystapil blad podczas proby pobrania z bazy");
		}
		return automats;
	}
	
	public static ArrayList<String> showAddreses(){
		ArrayList<String> automats = new ArrayList<String>();
		//ArrayList<String> names = new ArrayList<String>();
		try {
			Connection connection = new Connector().getConnection();
			Statement statement = connection.createStatement();
	

			ResultSet result = statement
					.executeQuery("SELECT address FROM automats.machines order by id");

			while (result.next()) {
				//String name = result.getString("machines.name");
				String address = result.getString("machines.address");
				automats.add(address);
				//names.add(name);
			
			}
			result.close();
			connection.close();
			
			
		} catch (Exception e) {
			e.printStackTrace();
			automats.add("Wystapil blad podczas proby pobrania z bazy");
		}
		return automats;
	}
	
	
	public static ArrayList<String> showNames(){
		ArrayList<String> automats = new ArrayList<String>();
		//ArrayList<String> names = new ArrayList<String>();
		try {
			Connection connection = new Connector().getConnection();
			Statement statement = connection.createStatement();
	

			ResultSet result = statement
					.executeQuery("SELECT name FROM automats.machines order by id");

			while (result.next()) {
				String name = result.getString("machines.name");
				//String address = result.getString("machines.address");
				automats.add(name);
				//names.add(name);
			
			}
			result.close();
			connection.close();
			
			
		} catch (Exception e) {
			e.printStackTrace();
			automats.add("Wystapil blad podczas proby pobrania z bazy");
		}
		return automats;
	}
	
}
